<?php

class PaymentUploadForm extends CFormModel{
    public $payment;
    public $paymentDetail;
    public $headerRow;
    public $processed=0;
    public function rules(){
        return array(
            array('payment,paymentDetail','file','maxSize'=>'20000000','allowEmpty'=>false,'types'=>'csv'),
            array('headerRow','safe')
        );
    }
    public function process(){
        //read 2 files, read each column, get each data, using import function
        ini_set("auto_detect_line_endings", true);
        $delimiter = ',';
        $this->processed = 0;
        $this->payment = CUploadedFile::getInstance($this, 'payment');        
        if($this->payment!=null){
            $this->payment->saveAs('payment/'.$this->payment->name);
        }
    
        $this->paymentDetail = CUploadedFile::getInstance($this, 'paymentDetail');
        if($this->paymentDetail!=null){
            $this->paymentDetail->saveAs('payment/'.$this->paymentDetail->name);
        }
        $paymentArray=array();
        $detailArray=array();
        $fPayment = fopen('payment/'.$this->payment, 'r');        
        if($fPayment!==false){
        
            $first= true;
            while($line = fgets($fPayment)){                
          
                if($first && $this->headerRow==1){
                    $first = false;                                                            
                    continue;
                }                   
                
                $lineArray = safeExplode($delimiter, $line);                    
                if(count($lineArray)<8){
                
                    return false;
                }else{
                    if(trim($lineArray[0])!='')
                        $paymentArray[$lineArray[0]]=$lineArray;
                    
                }
                
            }
        }else{
          
            return false;
        }
        
        $detailHandle = fopen('payment/'.$this->paymentDetail,'r');
        if($detailHandle!==false){
            $first=true;
            while($line = fgets($detailHandle)){
                if($first && $this->headerRow == 1){
                    $first=false;
                    continue;
                }
                $lineArray = safeExplode($delimiter,$line);
                
                if(count($lineArray)<5){
                    return false;
                }
                if(trim($lineArray[0])!='')
                    $detailArray[$lineArray[0]][]=$lineArray;
            }
        }
        var_dump($paymentArray);
        foreach($paymentArray as $payment){
            $paymentNo = $payment[0];
            $date = $payment[1];
            $dateArray = safeExplode('/', $date);
            var_dump($dateArray);
            $date = '20'.$dateArray[2].'-'.$dateArray[0].'-'.$dateArray[1];
         
            $ckno = $payment[2];
            $custno = $payment[4];
            $locno =$payment[5];
            $memo = $payment[6];
            $amount = floatval($payment[7]);
            if(isset($detailArray[$paymentNo])){
                
                $invoiceList = array();
                $totalDetailAmount=0;
                foreach($detailArray[$paymentNo] as $detail){
                    $totalDetailAmount +=floatval($detail[4]);
                    $invoiceList[] = array(
                        'invoice'=>$detail[2],
                        'appliedamount'=>floatval($detail[4]), 
                        'memo'=>$detail[3]
                    );
                }
                
                if($totalDetailAmount > $amount){
                    throw new CHttpException(404,'Payment amount can not lower than total detail amount, please check your file at payment number'.$paymentNo);
                }
                $paymentUtil = new PaymentUtil();
                $result = $paymentUtil->savePayment($invoiceList, $amount, 1, $ckno, $memo,$date);
                if($result == false){
                    throw new CHttpException(500,'Error when processing payment number '.$paymentNo);
                }else{
                    $this->processed++;
                }
            }else{
                throw new CHttpException(400,'Missing payment details at '.$paymentNo);
            }
        }
        //write it back to the csv
        
        return true;
        
        
    }
}